package org.logistics;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.logistics.bean.MapInforBean;
import org.logistics.bean.getMapInforMethod;
import org.logistics.R;



import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.LocationData;
import com.baidu.mapapi.map.MKMapViewListener;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.OverlayItem;
import com.baidu.mapapi.map.Symbol;
import com.baidu.mapapi.map.TextItem;
import com.baidu.mapapi.map.TextOverlay;
import com.baidu.mapapi.utils.CoordinateConvert;
import com.baidu.platform.comapi.basestruct.GeoPoint;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;
/**
 * 
 * @author yimou
 * 主界面
 */
public class MainMapActivity extends Activity {
	
	private BMapManager bMapManager = null ;
	/**
	 * 坐标对象集合
	 */
	List<GeoPoint> geoPointList = new ArrayList<GeoPoint>();
	List<OverlayItem > overLayList = new ArrayList<OverlayItem>();
	/**
	 * 文字显示集合
	 */
	List<TextItem> textItemList = new ArrayList<TextItem>();
	/**
	 * 坐标接口对象集合
	 */
	List<MapInforBean> mapInforList = new ArrayList<MapInforBean>();
	/**
	 * 包名
	 */
	final static String TAG = "org.MainMapActivity.logistics";
	/**
	 *  MapView 是地图主控件
	 */
	private MapView mMapView = null;
	/**
	 *  用MapController完成地图控制 
	 */
	private MapController mMapController = null;
	/**
	 *  MKMapViewListener 用于处理地图事件回调
	 */
	MKMapViewListener mMapListener = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		bMapManager = new BMapManager(this.getApplication());   
		bMapManager.init("B35aaf9987fd06500f9ae1170510ab94", null);
		/**
		 * 验证密钥是否正确
		 */
		new MKGeneralListener() {   
			public void onGetPermissionState(int error) {          
				Log.d(TAG, "onGetPermissionState， error: " + error);  

				}   
			public void onGetNetworkState(int error) {            
				Log.d(TAG, "onGetNetworkState， error: " + error);         
				} 
		};
		
		setContentView(R.layout.activity_main_map);
		
		
		//请求服务器 得到车辆信息
		getMapInforMethod mapInfor = new getMapInforMethod();
		try {
			mapInforList = mapInfor.getLocalPlace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		mMapView = (MapView)findViewById(R.id.bmapView);
		mMapView.setBuiltInZoomControls(true);
		 /**
        * 获取地图控制器
        */
       mMapController = mMapView.getController();
       /**
        *  设置地图是否响应点击事件  .
        */
       mMapController.enableClick(true);
       /**
        * 设置地图缩放级别
        */
       mMapController.setZoom(16);
       
       /**
        * 以一辆车自我定位中心
        */
       LocationData locData = new LocationData();  
       System.out.println("i="+mapInforList.get(2).getI()+" h="+mapInforList.get(2).getH());
       locData.latitude = (double)(mapInforList.get(2).getH());  
       locData.longitude = (double)(mapInforList.get(2).getI());  
       //locData.direction = 2.0f;  
       GeoPoint ep = new GeoPoint((int)(locData.latitude*1e6),  
    	       (int)(locData.longitude* 1e6));
       GeoPoint p = CoordinateConvert.fromWgs84ToBaidu(ep); 
      //有显示原点
//       myLocationOverlay.setData(locData);  
//       mMapView.getOverlays().add(myLocationOverlay);  
//       mMapView.refresh();  
//       mMapView.getController().animateTo(p);
       mMapController.setCenter(p);
       
       Drawable mark= getResources().getDrawable(R.drawable.nav_turn_via_1);
       
       changeTextObject();
       
       changeGeoPointObject(mark);
       
       OverLayClass itemOverlay = new OverLayClass(mark, mMapView) ;
       TextOverlay textOverlay = new TextOverlay(mMapView);
       
       //mMapView.getOverlays().clear();  
       mMapView.getOverlays().add(itemOverlay); 
       mMapView.getOverlays().add(textOverlay);
       
       int len = textItemList.size();
       for(int i = 0 ; i < len ; i++)
       {
    	   textOverlay.addText(textItemList.get(i));
       }
       itemOverlay.addItem(overLayList);
       mMapView.refresh(); 
       
       
       
		
		/**
		 * 地图生命周期
		 * 
		 */
		 mMapListener = new MKMapViewListener(){

			@Override
			public void onClickMapPoi(MapPoi arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onGetCurrentMap(Bitmap arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onMapAnimationFinish() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onMapLoadFinish() {
				Toast.makeText(MainMapActivity.this, 
					       "地图加载完成", 
					       Toast.LENGTH_SHORT).show();
				
			}

			@Override
			public void onMapMoveFinish() {
				
			}
			 
		 };
       
	}
	


	/**
	 * 回调处理函数
	 */
	@Override  
	protected void onDestroy(){  
	        mMapView.destroy();  
	        if(bMapManager!=null){  
	        	bMapManager.destroy();  
	        	bMapManager=null;  
	        }  
	        super.onDestroy();  
	}  
	@Override  
	protected void onPause(){  
	        mMapView.onPause();  
	        if(bMapManager!=null){  
	        	bMapManager.stop();  
	        }  
	        super.onPause();  
	}  
	@Override  
	protected void onResume(){  

	        mMapView.onResume();  
	        if(bMapManager!=null){  
	        	bMapManager.start();  
	        }  
	       super.onResume();  
	} 

	/**
	 * 转化文字坐标图层
	 */
	private void changeTextObject() {
		
		//颜色定义
		Symbol textSymbol = new Symbol();  
	   	Symbol.Color textColor = textSymbol.new Color();  
	   	textColor.alpha = 255;  
	   	textColor.red = 0;  
	   	textColor.blue = 255;  
	   	textColor.green = 0;  
	   	Symbol textSymbol1 = new Symbol();    
	   	Symbol.Color textColor1 = textSymbol1.new Color();    
	   	textColor1.alpha = 150;    
	   	textColor1.red = 80;    
	   	textColor1.blue = 80;    
	   	textColor1.green = 80;
	   	
	   	int len = mapInforList.size(); 
	   	for(int i = 0 ; i < len ; i++)
	   	{
	   		TextItem textItem = new TextItem();  
	   	   	textItem.fontColor = textColor;  
	   	   	textItem.bgColor = textColor1;  
	   	   	textItem.fontSize = 20;  
	   	   	String carNum = mapInforList.get(i).getCarNum().toString();
	   	   	double speed = mapInforList.get(i).getSpeed();
	   	   	textItem.text = carNum +"---"+ speed+"km/h";  
	   	   	textItem.pt =CoordinateConvert.fromWgs84ToBaidu(new GeoPoint((int) (mapInforList.get(i).getH() * 1E6)
	   	   			, (int) (mapInforList.get(i).getI() * 1E6))); 
	   	   	textItemList.add(textItem);
	   	}
	   	
	   	
	}
	

	
	/**
	 * 把接口数据集合转换成GeoPoint对象集合
	 */
	public void changeGeoPointObject (Drawable mark)
	{
		 int len = mapInforList.size();
	       for(int i = 0 ; i < len ; i++ )
	       {
	    	   double mLat = (double) mapInforList.get(i).getH();
	    	   double mLon = (double) mapInforList.get(i).getI();
	    	   Log.i(TAG, "h="+mLat+" i="+mLon);
	    	   GeoPoint gp = new GeoPoint((int) (mLat * 1E6), (int) (mLon * 1E6));
	    	   GeoPoint p = CoordinateConvert.fromWgs84ToBaidu(gp);  
	    	   geoPointList.add(p);
	       }
	       for(int j = 0 ; j < len ; j++ )
	       {
	    	   OverlayItem item = new OverlayItem(geoPointList.get(j), "item"+j, "item"+j);
	    	   item.setMarker(mark);
	    	   overLayList.add(item);
	       }
	}
	
	/**
	 * 系统自建
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main_map, menu);
		return true;
	}

}
